package com.hs.srm.admin.modules.sampletest.controller;

import com.hs.srm.admin.modules.orders.vo.ScmPoItemVo;
import com.hs.srm.admin.modules.sampletest.service.SampleScmPoItemService;
import com.hs.srm.admin.modules.sys.controller.AbstractController;
import com.hs.srm.base.common.config.SysConfig;
import com.hs.srm.base.common.utils.EntityUtils;
import com.hs.srm.base.modules.sys.entity.SysUserEntity;
import com.hs.srm.common.utils.PageUtils;
import com.hs.srm.common.utils.R;
import com.hs.srm.tools.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 * @Description: 品管试用
 * @Author yangch
 * @Date 2019/7/2
 *
 */

@RestController
@RequestMapping("/qualityControlTest")
public class QualityControlTestController extends AbstractController {

    @Autowired
    private SampleScmPoItemService sampleScmPoItemService;

    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        SysUserEntity username = this.getUser();
        if(username.getDeptId()==11){
            params.put("username",username.getUsername());
        }
        PageUtils pu = this.sampleScmPoItemService.selectDetailPage(params);
        List<ScmPoItemVo> voList = (List<ScmPoItemVo>)pu.getList();
        voList = EntityUtils.valToTxt(voList);
        pu.setList(voList);
        return R.ok(pu);
    }

    //报告上传
    @RequestMapping(value="/upload",method = RequestMethod.POST)
    public R upload(HttpServletRequest request) throws Exception{
        MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest) request;
        //上传报告
        MultipartFile multipartFile = multipartRequest.getFile("reportFile");
        //上传文件名添加时间后缀 ！！！！！！！！！！！！！！！！！！！！！！！不要改时间后缀格式，不要改、不要改！！！！！！！！！！
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("_yyyy-MM-dd HH~mm~ss");
        String StrDate = simpleDateFormat.format(new Date());
        String originalFilename=multipartFile.getOriginalFilename();
        //文件保存路径
        String fileDir= SysConfig.getUploadPath()+ "/sampletest/";
        String fileName=originalFilename.substring(0,originalFilename.lastIndexOf("."))+StrDate+"."+originalFilename.substring(originalFilename.lastIndexOf(".")+1);
        String filePath=fileDir+fileName;
        boolean result= FileUtils.fileUpload(fileDir,fileName, multipartFile);
        //保存失败
        if(!result){
            return  R.error();
        }
        boolean pu = sampleScmPoItemService.saveUploadPathRemark(request,filePath);
        return R.ok(pu);
    }

    //报告下载
    @RequestMapping("/download")
    public R download(@RequestParam(value = "files") String[] filePath, HttpServletResponse response,HttpServletRequest request) throws Exception{
        //文件下载，多文件打包zip下载,单文件直接下载
        if(filePath.length>1){
            FileUtils.zipDownload(filePath,"文件下载",request,response);
        }else{
            FileUtils.fileDownload(filePath[0],request,response);
        }
        return R.ok();
    }

}
